package com.example.gzysmartcity.viewmodel;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.example.gzysmartcity.repository.model.MyTravel;
import com.example.gzysmartcity.repository.model.SCJourney;
import com.example.gzysmartcity.repository.model.SCScenerySpot;
import com.example.gzysmartcity.repository.model.SCTravel;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.utils.NetworkUtil;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class TravelViewModel extends ViewModel {
    private MutableLiveData<State> mutableLiveData;
    private List<SCTravel.DataBean> scTravelList;
    private List<SCScenerySpot.DataBean> scScenerySpotList;
    private List<SCJourney.DataBean> scJourneyList;
    private List<MyTravel> myTravelList;

    public void search(List<SCTravel.DataBean> list, String name) {
        list.clear();
        if (name.equals("")) {
            list.addAll(scTravelList);
        } else {
            list.addAll(scTravelList.stream().filter(a -> a.getName().contains(name)).collect(Collectors.toList()));
        }
    }

    /**
     * 查询景点所有数据
     */
    public void getTravel() {
        mutableLiveData = new MutableLiveData<>();
        ThreadUtils.executeByCached(new ThreadUtils.Task<SCTravel>() {
            @Override
            public SCTravel doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("SCTravel/getAll?", null, SCTravel.class, 0);
            }

            @Override
            public void onSuccess(SCTravel result) {
                scTravelList = result.getData();
                getSCJourney();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
                mutableLiveData.setValue(State.FAIL);
            }
        });
    }

    /**
     * 查询行程所有数据
     */
    private void getSCJourney() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<SCJourney>() {
            @Override
            public SCJourney doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("SCJourney/getAll?", null, SCJourney.class, 0);
            }

            @Override
            public void onSuccess(SCJourney result) {
                getSCScenerySpot();
                scJourneyList = result.getData();

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    public String getJourney(int id) {
        StringBuilder stringBuilder = new StringBuilder();
        Set<String> set = new HashSet<>();
        scJourneyList.stream().filter(a -> a.getSCTravelId() == id).sorted(Comparator.comparingInt(SCJourney.DataBean::getOrderBy))
                .forEach(a -> {
                    if (set.add(a.getDay())) {
                        stringBuilder.append("<p class=\"txt01\" style=\"margin-top: 0px; margin-bottom: 0px; padding: 0px; background: rgb(238, 238, 238); font-size:\n" +
                                "\t12px; font-weight: 700; color: rgb(153, 153, 153); height: 17px; line-height: 17px;\">Day</p>\n" +
                                "<p class=\"day_num\" style=\"margin-top: 0px; margin-bottom: 0px; padding: 0px; font-weight: 700; font-size: 18px;\n" +
                                "\tcolor: rgb(102, 102, 102); line-height: 21px; border-right: 2px solid rgb(238, 238, 238); border-bottom: 2px solid\n" +
                                "\trgb(238, 238, 238); border-left: 2px solid rgb(238, 238, 238); border-image: initial; border-top: none;\">0")
                                .append(a.getDay()).append("</p>");
                    }
                    stringBuilder.append("<p class=\"time_num\" style=\"margin-top: 0px; margin-bottom: 0px; padding: 0px; font-size: 13px;line-height: 18px; font-weight: 700;\">")
                            .append(a.getHowLong()).append("</p>")
                            .append(a.getContent());
                });
        return stringBuilder.toString();
    }

    /**
     * 查询景点所有数据
     */
    private void getSCScenerySpot() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<SCScenerySpot>() {
            @Override
            public SCScenerySpot doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("SCScenerySpot/getAll?", null, SCScenerySpot.class, 0);
            }

            @Override
            public void onSuccess(SCScenerySpot result) {
                scScenerySpotList = result.getData();
                ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
                    @Override
                    public Object doInBackground() {
                        myTravelList = new ArrayList<>();
                        for (SCTravel.DataBean dataBean : scTravelList) {
                            Set<Integer> set = new HashSet<>();
                            ArrayList<String> imgList = new ArrayList<>();
                            scJourneyList.stream().filter(a -> a.getSCTravelId() == dataBean.getId())
                                    .forEach(a -> {
                                        if (a.getSCScenerySpotId() != 0) {
                                            set.add(a.getSCScenerySpotId());
                                        }
                                    });
                            for (Integer integer : set) {
                                SCScenerySpot.DataBean scScenerySpot = scScenerySpotList.stream().filter(a -> a.getId() == integer).findFirst().get();
                                Document parse = Jsoup.parse(scScenerySpot.getContent());
                                for (Element img : parse.getElementsByTag("img")) {
                                    String src = img.attr("src");
                                    if (!src.equals("")) {
                                        imgList.add(src);
                                        break;
                                    }
                                }
                                if (imgList.size() == 5) break;
                            }
                            myTravelList.add(new MyTravel(imgList));
                            Log.i("TAG", "doInBackground: " + imgList.size());
                        }
                        return null;
                    }

                    @Override
                    public void onSuccess(Object result) {
                        mutableLiveData.setValue(State.SUCCESS);
                    }
                });
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    public List<MyTravel> getMyTravelList() {
        return myTravelList;
    }

    public List<SCTravel.DataBean> getScTravelList() {
        return scTravelList;
    }

    public LiveData<State> getMutableLiveData() {
        return mutableLiveData;
    }
}
